package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallOrderGoodsMapper;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsExample;
import org.linlinjava.litemall.db.mapper.LitemallOrderGoodsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallOrderGoodsService {
    @Resource
    private LitemallOrderGoodsDao orderGoodsMapper;

    public int add(LitemallOrderGoods orderGoods) {
        orderGoods.setAddTime(LocalDateTime.now());
        orderGoods.setUpdateTime(LocalDateTime.now());
        orderGoodsMapper.insertTemplate(orderGoods,true);
        return 1;
        //return orderGoodsMapper.insertSelective(orderGoods);
    }

    public List<LitemallOrderGoods> queryByOid(Integer orderId) {
    	
    	LitemallOrderGoods  query = new LitemallOrderGoods();
    	query.setOrderId(orderId);
    	query.setDeleted(false);
    	return orderGoodsMapper.template(query);
		/*
		 * LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
		 * example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false); return
		 * orderGoodsMapper.selectByExample(example);
		 */
    }

    public List<LitemallOrderGoods> findByOidAndGid(Integer orderId, Integer goodsId) {
    	
    	LitemallOrderGoods  query = new LitemallOrderGoods();
    	query.setOrderId(orderId);
    	query.setGoodsId(goodsId);
    	query.setDeleted(false);
    	return orderGoodsMapper.template(query);
		/*
		 * LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
		 * example.or().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId).
		 * andDeletedEqualTo(false); return orderGoodsMapper.selectByExample(example);
		 */
    }

    public LitemallOrderGoods findById(Integer id) {
    	return orderGoodsMapper.single(id);
       // return orderGoodsMapper.selectByPrimaryKey(id);
    }

    public void updateById(LitemallOrderGoods orderGoods) {
        orderGoods.setUpdateTime(LocalDateTime.now());
        orderGoodsMapper.updateTemplateById(orderGoods);
        //orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);
    }

    public Short getComments(Integer orderId) {
    	LitemallOrderGoods  query = new LitemallOrderGoods();
    	query.setOrderId(orderId);
    	query.setDeleted(false);
    	int count = (int)orderGoodsMapper.templateCount(query);
    	 return (short) count;
			/*
			 * LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
			 * example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false); long count
			 * = orderGoodsMapper.countByExample(example); return (short) count;
			 */
    }

    public boolean checkExist(Integer goodsId) {
    	
    	LitemallOrderGoods  query = new LitemallOrderGoods();
    	query.setGoodsId(goodsId);
    	query.setDeleted(false);
    	int count = (int)orderGoodsMapper.templateCount(query);
    	 return count!=0;
    	
			/*
			 * LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
			 * example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false); return
			 * orderGoodsMapper.countByExample(example) != 0;
			 */
    }

    public void deleteByOrderId(Integer orderId) {
    	
    	LitemallOrderGoods  query = new LitemallOrderGoods();
    	query.setOrderId(orderId);
    	query.setDeleted(true);
    	orderGoodsMapper.updateTemplateById(query);
		/*
		 * LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
		 * example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
		 * orderGoodsMapper.logicalDeleteByExample(example);
		 */
    }
}
